package edu.sjsu.cs.phan.hw6;

/** COPYRIGHT(C) 2008. David J. Barnes and Michael Kolling.
 * All Rights Reserved.
 * TicketMachine models a naive ticket machine that issues
 * flat-fare tickets.
 * The price of a ticket is specified via the constructor.
 *
 * @author David J. Barnes and Michael Kolling
 * @version 1.01 2008.03.30
 */
public class TicketMachine
{

   /**
     * Create a machine that issues tickets of the given price.
     * Note that the price must be greater than zero, and there
     * are no checks to ensure this.
     */
    public TicketMachine(int ticketCost)
    {
//        int price = ticketCost;
//        int balance = 0;
//        int total = 0;
       /*   Corrected by Loc Phan 11/26/08
            Error: balance, total and price
            are already initialized and needed to
            be set to correct number.*/
       price = ticketCost;
       balance = total = remainingbalance = 0;
    }

    /**
     * Return the price of a ticket.
     */
    public int getPrice()
    {
        return price;
    }

    /**
     * Return the amount of money already inserted for the
     * next ticket.
     */
    public int getBalance()
    {
        return balance;
    }

    /**
     * Receive an amount of money in cents from a customer.
     */
    public void insertMoney(int amount)
    {
        balance = balance + amount;
        remainingbalance = balance;
    }

    /**
     * Print a ticket.
     * Update the total collected and
     * reduce the balance to zero.
     */
    public void printTicket()
    {
      /*
        // Simulate the printing of a ticket.
        System.out.println("##################");
        System.out.println("# The BlueJ Line");
        System.out.println("# Ticket");
        System.out.println("# " + price + " cents.");
        System.out.println("##################");
        System.out.println();       
        */
       /*Corrected by Loc Phan 11/26/08
        * Error: will print ticket even though
        * balance < price
        */       
       if(remainingbalance >= price)
       {
          System.out.println("##################");
          System.out.println("# The BlueJ Line");
          System.out.println("# Ticket");
          System.out.println("# " + price + " cents.");
          System.out.println("##################");
          System.out.println();
          remainingbalance = remainingbalance - price;

       }
       
       // Update the total collected with the balance.
       total = total + balance;
       // Clear the balance.
       balance = 0;
    }
    public int RemainingBalance()
    {
       return remainingbalance;
    }
    // The price of a ticket from this machine.
    private int price;
    // The amount of money entered by a customer so far.
    private int balance;
    // The total amount of money collected by this machine.
    private int total;
    
    private int remainingbalance;
}
